<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>消息发送</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
    <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/css/public.css">
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
</head>
<body>

<div class="layui-fluid "style="padding: 10px;position: absolute;left: 0;overflow: auto;top: 0; bottom: 0;right: 0;">
    <div class="layui-row">
        <div class="layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <a href="#/system/messageSend/view/form" title="返回">
                        <i class="layui-icon layui-icon-return"></i>
                    </a>
                    <span class="indexColumnTitle">发送新信息</span>
                </div>
                <div class="layui-card-body">

<form class="layui-form sendViewForm" lay-filter="sendViewForm"
      style="height:100%;margin:1px 1px 1px 1px;padding:20px 28px 20px 0;background: #fff">
    
    <div class="layui-form-item">
        <label class="layui-form-label">标题:</label>
        <div class="layui-input-block"style="width:45%">
            <input type="text" name="subject" lay-verify="subject" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">

        <label class="layui-form-label">收信人:</label>
        <div class="layui-input-block" style="width: 90%" >
            <div  style="width:50%;display: inline-block">
                <input   readonly="readonly" type="text" name="receiverName"
                         lay-verify="receiverName" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn selectUser" inputIndex="1" type="button">选择</button>
            <button class="layui-btn cleanUser" type="button">清空</button>
        </div>
    </div>




    <div class="layui-form-item">

        <label class="layui-form-label">收信组织:</label>
        <div class="layui-input-block" style="width: 90%">
            <div style="width:50%;display: inline-block">
                <input type="text"  readonly="readonly"
                       name="receiverOrgName" lay-verify="receiverOrgName" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn selectOrg" inputIndex="1" type="button">选择</button>
            <button class="layui-btn cleanOrg" type="button">清空</button>

        </div>
    </div>
   <!-- <div class="layui-form-item">
        <label class="layui-form-label">附件:</label>
        <div class="layui-input-block">
            <input type="text" name="attachment" lay-verify="attachment" autocomplete="off" class="layui-input">
        </div>
    </div>-->

    <div class="layui-form-item">
        <label class="layui-form-label">需要回复：</label>
        <div class="layui-input-block">
            <input type="radio" name="canReply" value="1" checked="" title="是"/>
            <input type="radio" name="canReply" value="0" title="否"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">内容:</label>
        <div class="layui-input-block"style="width:90%">
            <textarea style="height:300px" id="content" name="content" id="messageUrl" style="display:none" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center;margin: 0">
        <div class="layui-input-block" style="text-align: center;margin: 0">
            <button class="layui-btn add" lay-submit lay-filter="add" type="submit">发送</button>
            <a class="layui-btn layui-btn-danger cancelAddedit">返回</a>
        </div>
        <input type="hidden" name="id"/>
        <input type="hidden" name="messageType" value="1"/>
        <input type="hidden" name="receiverId">
        <input type="hidden" name="receiverOrgId">
    </div>
</form>
</div>
                </div>
            </div>
        </div>
    </div>
<script src="${ctx}/public/component.js" type="text/javascript"></script>
<script src="${ctx}/layui/layui.js"></script>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'layer', 'setter', 'commons', 'common', 'totalFilter', 'form', 'jquery', 'upload', 'layedit'], function () {
        var table = layui.table, layer = layui.layer, setter = layui.setter, commons = layui.commons, common = layui.common, totalFilter = layui.totalFilter, form = layui.form, $ = layui.jquery;var layedit = layui.layedit;
        //初始化富文本剪辑器
        var index = layedit.build('content',{
            tool: ['strong' //加粗
                ,'italic' //斜体
                ,'underline' //下划线
                ,'del' //删除线
                ,'|' //分割线
                ,'left' //左对齐
                ,'center' //居中对齐
                ,'right' //右对齐
                ,'face' //表情
         ]
        });

        $(".cancelAddedit").click(function () {
            $(this).attr("href", serverPath.systemPath+setter.sysLinkPath+"messageSend/view/form");
        });

        //表单渲染
        var indexOne = parent.layer.getFrameIndex(window.name);
        form.render();
        //表单验证
        form.verify({
            subject: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (value == null || value == "") {
                    return '标题不能为空';
                }

            },
            receiverName: function (value, item) { //value：表单的值、item：表单的DOM对象
                if ((value == null || value == "")&&($("input[name='receiverOrgName']").val()==null||$("input[name='receiverOrgName']").val()=="")) {
                    return '收信人或收信组织不能为空';
                }

            }
        });
        //监听表单提交
        form.on('submit(add)', function (data) {

            data.field.content = layedit.getContent(index);
            $.ajax({
                url: serverPath.systemPath + setter.sysLinkPath + "messageSend/add",
                type: "POST",
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(data.field),
                success: function (data) {
                    if (commons._filterRecord(data)) {
                        layer.alert(data.msg, function (index) {
                            window.parent.location.reload();//刷新父页面
                        });
                    } else {
                        layer.alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("保存失败！" + XMLHttpRequest.status);
                }
            });
            return false;
        });


        //人员多选选择器
        layui.use(['layer', 'setter'], function () {
            var setter = layui.setter
                , layer = layui.layer;
            var userSelectorWin;
            $('.selectUser').click(function () {
                var theData = $(this).siblings("div").find("input[name=receiverName]");
                var hiddenData = $(this).siblings("div").find("input[name=receiverId]");
                var body;
                layer.open({
                    type: 2
                    , title: '人员选择器'
                    , area: ['980px', '630px']
                    ,btn:["保存","取消","撤销"]
                    , resize: false
                    , content: serverPath.systemPath + setter.sysLinkPath+'dialog/view/sysUserDialog'
                    , success: function (layero, index) {
                        body = layer.getChildFrame('body',index);
                        userSelectorWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        //向用户选择器页面传递初始值[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                        if (theData.val()) {
                            userSelectorWin.layui.userList = [];
                            var hiddenDataArr = hiddenData.val().split(",");
                            var theDataArr = theData.val().split(",");
                            $.each(hiddenDataArr,function (k,v) {
                                var userListObj={};
                                userListObj.name = theDataArr[k];
                                userListObj.userId = v;
                                userSelectorWin.layui.userList.push(userListObj);
                            })
                        }
                    }
                    ,yes:function(index,layero){
                        //用户选择器返回的选择人员数组[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                        var theUserList = body.find("input[name=userList]").val();
                        dateReturn(JSON.parse(theUserList));
                        layer.close(index);
                    },
                    btn3:function(index, layero){
                        userSelectorWin.cancelData(index);
                        return false //开启该代码可禁止点击该按钮关闭
                    }
                    // , end: function () {
                    //     dateReturn(userSelectorWin.window.layui.userList);
                    // }
                });
            })
            // 给“人员选择器”初始化userList
            function initUserList(){
                return $.map(userSelectorWin.ownerList,function(v){
                    return {name:v.ownerName,userId:v.ownerId};
                })
            }
        });
        //组织多选选择器
        layui.use(['layer', 'setter'], function () {
            var setter = layui.setter
                , layer = layui.layer;
            $('.selectOrg').click(function () {
                var theData = $(this).siblings("div").find("input[name=receiverOrgName]");
                var hiddenData = $(this).siblings("div").find("input[name=receiverOrgId]");
                var orgSelectorWin;
                var body;
                layer.open({
                    type: 2
                    , title: '组织选择器'
                    , btn:["保存","取消","撤销"]
                    , area: ['980px', '630px']
                    , resize: false
                    , content: serverPath.systemPath + setter.sysLinkPath+'dialog/view/sysOrgDialog'
                    , success: function (layero, index) {
                        body = layer.getChildFrame('body',index);
                        orgSelectorWin = window[layero.find('iframe')[0]['name']];
                        //向用户选择器页面传递初始值[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                        if (theData.val()) {
                            orgSelectorWin.layui.orgList = [];
                            var hiddenDataArr = hiddenData.val().split(",");
                            var theDataArr = theData.val().split(",");
                            $.each(hiddenDataArr,function (k,v) {
                                var userListObj={};
                                userListObj.name = theDataArr[k];
                                userListObj.orgId = v;
                                orgSelectorWin.layui.orgList.push(userListObj);
                            })
                        }
//                        name: "wang2"
//​
//userId: "55a517a53e9e441ba8885b3369fa6fe4"
                    },
                    yes: function(index, layero){
                        //用户选择器返回的选择人员数组[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                        var theOrgList = body.find("input[name=orgList]").val();
                        console.log(JSON.parse(theOrgList));
                        orgDateReturn(JSON.parse(theOrgList));
                        layer.close(index);
                    },
                    btn3:function(index, layero){
                        orgSelectorWin.cancelData(index);
                        return false //开启该代码可禁止点击该按钮关闭
                    }
                });
            })
        });
        //组织选择器值回填
        function orgDateReturn(obj) {
            var receiverOrgName = '';
            var receiverOrgId = '';
            for (var c in obj) {
                if (c == 0) {
                    receiverOrgId = obj[c].orgId;
                    receiverOrgName = obj[c].name;
                } else {
                    receiverOrgName = receiverOrgName + ',' + obj[c].name;
                    receiverOrgId=  receiverOrgId + ',' + obj[c].orgId;
                }

            }
            $("input[name='receiverOrgName']").attr("value", receiverOrgName);
            $("input[name='receiverOrgId']").attr("value", receiverOrgId);
        }
        //人员选择器值回填
        function dateReturn(obj) {
            var receiverName = '';
            var receiverId = '';
            for (var c in obj) {
                if (c == 0) {

                    receiverId = obj[c].userId;
                    receiverName = obj[c].name;
                } else {
                    receiverName = receiverName + ',' + obj[c].name;
                    receiverId = receiverId + ',' + obj[c].userId;
                }
            }

            $("input[name='receiverName']").attr("value", receiverName);
            $("input[name='receiverId']").attr("value", receiverId);
        }
        //清空人员选择
        $(".cleanUser").click(function () {
            $("input[name='receiverName']").attr("value", '');
            $("input[name='receiverId']").attr("value", '');
        })
        //清空组织选择
        $(".cleanOrg").click(function () {
            $("input[name='receiverOrgName']").attr("value", '');
            $("input[name='receierOrgId']").attr("value", '');
        })
        $(".cancelView").click(function () {
            parent.layer.close(indexOne);
        })
    });
</script>
</body>
</html>